<?php
session_start();
include_once "../apps/ManagerIUDS.php";
$managerIUDS = new ManagerIUDS();

$medicineName = $_POST["medicine_name"];
$medicineDetail = $_POST["medicine_detail"];
$medicineType = $_POST["medicine_type"];
$medicineSubType = $_POST["medicine_sub_type"];
$amount = $_POST["amount"];
$price = $_POST["price"];

$medicineId = $_GET["medicine_id"];
if($medicineId == "" || $medicineId == null){
	$medicineId = $_POST["medicine_id"];
}

$mode = $_GET["mode"];
if($mode == "" || $mode == null){
	$mode = $_POST["mode"];
}

$page = $_GET['page'];
if (!isset($page) && $page == "") {
	$page = 1;
}

if($mode == "insert"){
	$arrData["medicine_name"] = "".$medicineName."";
	$arrData["medicine_detail"] ="".$medicineDetail."";
	$arrData["medicine_type"] ="".$medicineType."";
	$arrData["medicine_sub_type"] ="".$medicineSubType."";
	$arrData["price"] ="".$price."";
	$arrData["amount"] ="".$amount."";
	$arrData["create_date"] = "".date("Y-m-d")."";
	$arrData["create_by"] = "".$_SESSION["user_name"]."";
	$managerIUDS->inserData($arrData, "medicine");
	
	$medicineData = $managerIUDS->queryData(" MAX(medicine_id) as medicine_id", "medicine", "", "1", "");
	if(sizeOf($medicineData>0)){
	$arrHisData["medicine_id"] = "".$medicineData["medicine_id"]."";
	$arrHisData["amount"] ="".$amount."";
	$arrHisData["price"] ="".$price."";
	$arrHisData["history_type"] ="I"; //E
	$arrHisData["create_date"] = "".date("Y-m-d")."";
	$arrHisData["create_by"] = "".$_SESSION["user_name"]."";
	$managerIUDS->inserData($arrHisData, "history_medicine");
	}
	header("Location:../contents/medicine_main.php");
}else if($mode == "delete"){
	$arrPrimaryKey["medicine_id"] = "".$medicineId."";
	$managerIUDS->deleteData("medicine", $arrPrimaryKey, "");
	$managerIUDS->deleteData("history_medicine", $arrPrimaryKey, "");
	header("Location:../contents/medicine_main.php");
}else if($mode == "update"){
	$addAmount = $_POST["add_amount"];
	$sumAmount = $addAmount+$amount;
	$arrData["medicine_name"] = "".$medicineName."";
	$arrData["medicine_detail"] ="".$medicineDetail."";
	$arrData["medicine_type"] ="".$medicineType."";
	$arrData["medicine_sub_type"] ="".$medicineSubType."";
	$arrData["price"] ="".$price."";
	$arrData["amount"] ="".$sumAmount."";
	$arrData["update_date"] = "".date("Y-m-d")."";
	$arrData["update_by"] = "".$_SESSION["user_name"]."";
	$arrPrimaryKey["medicine_id"] = "".$medicineId."";
	//print_r($arrData);
	$managerIUDS->updateData($arrData, "medicine", $arrPrimaryKey, "");
	header("Location:../contents/medicine_main.php");
}else if($mode == "search"){
	$medicineName = $_GET["medicine_name"];
	header("Location:../contents/medicine_main.php?mode=search&page=".$page."&medicine_name=".urlencode($medicineName));
}else if($mode == "listMedicineData"){
	$medicineData = array();
	$medicineName = $_GET["medicine_name"];
	if($medicineName == "" || $mode == null){
		$medicineName = $_POST["medicine_name"];
	}
	$medicineData = $managerIUDS->queryData("medicine_name", "medicine", " WHERE medicine_name LIKE '%".$medicineName."%'", "2", "");
	echo json_encode($medicineData);
}else if($mode == "chaeckAmount"){
	$result = array();
	$amount = $_GET["amount"];
	$amounData = "";
	$medicineId = $_GET["medicineId"];
	if($amount == "" || $amount == null){
		$amount = $_POST["amount"];
	}
	$amounData = $managerIUDS->queryData("amount", "medicine", " WHERE medicine_id = '".$medicineId."'", "1", "");
	if($amounData["amount"] < $amount){
			$result = array('result' => "M");
	}else{
			$result = array('result' => "L");
	}
	echo json_encode($result);

}
?>